package k.index.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yc.bean.Result;
import k.daimai.entity.DmCategory;
import k.daimai.entity.DmProduct;
import k.daimai.entity.DmUser;
import k.index.Controller.feign.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class indexController {
//    public static Map<String,Object> map=new HashMap<>();
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    OrderFeign orderFeign;
    @GetMapping("index")
    public String getIndex(HttpServletRequest request) {
        return "index"+request.getServerPort();
    }
    @GetMapping("hots")
    public List<DmProduct> getOrder() throws JsonProcessingException {
        List<DmProduct> products=orderFeign.queryHots();
        // 将 List<DmProduct> 转换为 JSON 字符串
        String productListJson = objectMapper.writeValueAsString(products);
        redisTemplate.opsForValue().set("hots", productListJson);
        return products;

    }
    @GetMapping("getCategories")
    public List<DmCategory> queryCategories(){
        return orderFeign.queryCategories();
    }





    /// ////////////////////////////
    @RequestMapping("add")
    int add(@RequestParam("a") int a,@RequestParam("b") int b) {
        return orderFeign.add(a,b);
    }
    @RequestMapping("getResult")
    Result getResult( Result result) {
        return orderFeign.getResult(result);
    }
    @RequestMapping("user")
    DmUser getDmUser(@RequestBody DmUser user,@RequestParam("myname") String myname) {
        return orderFeign.getDmUser(user,myname);
    }
    @RequestMapping("hello")
    String hello(@RequestParam("name") String name) {
        return orderFeign.hello(name);
    }
}
